package com.hepinghospital.user.ui.fragment;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseFragment;
import com.hepinghospital.user.ui.PayActivity;
import com.hepinghospital.user.ui.widget.CustomPopWindow;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.DicResponse;
import com.rest.response.Doctor;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;
import jz.joyoung.robot.util.UIConstants;

/**
 * Created by HP on 2020/2/20.
 */

public class BookDoctorByNameFragment extends BaseFragment {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView doctor_recyclerView;
    @BindView(R.id.btn_zhichen)
    TextView btn_zhichen;
    @BindView(R.id.btn_keshi)
    TextView btn_keshi;

    private CustomPopWindow mPopWindow;
    List<DicResponse> list = new ArrayList<>();

    List<Doctor> doctorList = new ArrayList<>();
    DoctorAdapter doctorAdapter;
    AddMedicineAdapter adapter;
    String deptId;

    public static BookDoctorByNameFragment newInstance() {
        Bundle args = new Bundle();
        BookDoctorByNameFragment fragment = new BookDoctorByNameFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_book_docot_name;
    }

    @Override
    protected void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        doctor_recyclerView.setLayoutManager(manager);
        doctorAdapter = new DoctorAdapter();
        doctor_recyclerView.setAdapter(doctorAdapter);
        doctor_recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(mContext, 16);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> { });
    }

    @Override
    protected void initData() {
        deptId = getArguments().getString("deptId");
        getDoctorList("", "", "");
    }

    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(mContext).inflate(R.layout.item_book_doctor_online, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundleCloud = new Bundle();
                    bundleCloud.putSerializable(UIConstants.OBJ_Doctor, doctorList.get(position));
                    bundleCloud.putInt(UIConstants.PayActivity_TYPE_FLAG, UIConstants.PayActivity_TYPE_1);
                    startActivity(PayActivity.class, bundleCloud);
                }
            });
            holder.tv_level.setText(doctorList.get(position).jobTitleName);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
            holder.tv_star.setText(doctorList.get(position).score);
            holder.tv_name.setText(doctorList.get(position).docName);
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(mContext, doctorList.get(position).thumbnailUrl, holder.img_head);
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_skill, tv_star;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_star = itemView.findViewById(R.id.tv_star);
                img_head = itemView.findViewById(R.id.img_head);
            }
        }
    }

    private void getDoctorList(String content, String date, String time) {
//        RestProxy.getInstance().getCloudDoctorList(content,date, time, deptId, new Observer<CloudDoctorResponse>() {
//            @Override
//            public void onSubscribe(Disposable d) {
//
//            }
//
//            @Override
//            public void onNext(CloudDoctorResponse doctorResponse) {
//                doctorList.clear();
//                doctorList.addAll(doctorResponse.data.records);
//                doctorAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                refreshLayout.finishRefresh(false);
//            }
//
//            @Override
//            public void onComplete() {
//                refreshLayout.finishRefresh(true);
//            }
//        });
    }


    @OnClick(R.id.btn_keshi)
    void btn_keshi() {
        showPopListView(btn_keshi, 0);
    }

    @OnClick(R.id.btn_zhichen)
    void btn_zhichen() {
        showPopListView(btn_zhichen, 1);

    }

    private void showPopListView(View view, int type) {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_window, null);
        RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new AddMedicineAdapter(type);
        recyclerView.setAdapter(adapter);
//        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
//            @Override
//            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
//                super.onDraw(c, parent, state);
//            }
//
//            @Override
//            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//                super.getItemOffsets(outRect, view, parent, state);
//                outRect.bottom = DpUtil.dip2px(activity, 1);
//            }
//        });
        if (type == 0) {
            getDictList();
        } else {
            getDepartmentList();
        }

        //创建并显示popWindow
        mPopWindow = new CustomPopWindow.PopupWindowBuilder(getActivity())
                .setView(contentView)
                .setTouchable(true)
                .size(ViewGroup.LayoutParams.MATCH_PARENT, DpUtil.dip2px(mContext, 300)) // 显示大小
                .create()
                .showAsDropDown(view, 0, 0, Gravity.CENTER);
    }


    class AddMedicineAdapter extends RecyclerView.Adapter<AddMedicineAdapter.Holder> {

        int type;

        public AddMedicineAdapter(int type) {
            this.type = type;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(getActivity()).inflate(R.layout.item_textview, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (type == 0) {
                        btn_zhichen.setText(list.get(position).name);
                        getDoctorList(list.get(position).name, "", "");
                        mPopWindow.dissmiss();
                    } else {
                        btn_keshi.setText(list.get(position).name);
                        getDoctorList(list.get(position).name, "", "");
                        mPopWindow.dissmiss();
                    }
                }
            });
            holder.tv_name.setText(list.get(position).name);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.text);
            }
        }
    }

    private void getDictList() {
        RestProxy.getInstance().getDicList(new BaseObserver<List<DicResponse>>() {

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onSuccess(List<DicResponse> result) {
                list.clear();
                list.addAll(result);
                adapter.notifyDataSetChanged();
            }
        });
    }


    private void getDepartmentList() {
        RestProxy.getInstance().getDepartment2List(new Observer<List<DicResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<DicResponse> dicResponses) {
                list.clear();
                list.addAll(dicResponses);
                adapter.notifyDataSetChanged();
            }


            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
                ToastUtil.onError(mContext, e);

            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }
}
